package com.time.company.components.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.time.company.R;
import com.time.company.app.AddressManager;
import com.time.company.app.MyApplication;
import com.time.company.base.BaseActivity;
import com.time.company.components.MainActivity;
import com.time.company.servermodel.BasicBean;
import com.time.company.servermodel.MyAddress;
import com.time.company.utils.NetWorkUtils;
import com.time.company.utils.ScreenUtils;
import com.time.company.utils.ToastUtils;
import com.time.company.webserver.helper.WebServerManager;
import com.time.company.webserver.helper.WebServerUrlBuilder;
import com.time.company.webserver.request.BasicRequest;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 欢迎页
 * Created by Bing on 2017/10/18.
 * E-mail is tx16bing@163.com
 */

public class WelcomeActivity extends BaseActivity {

    private ImageView ivLaunchPage;
    private MyHandler handler = new MyHandler(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ivLaunchPage = findView(R.id.iv_launch_page);
    }

    @Override
    public void onResume() {
        super.onResume();
        initRequest();
    }

    @Override
    public boolean needTranslucentStatus() {
        return true;
    }

    // 进行地址获取
    private void initRequest() {
        if (NetWorkUtils.isNetAvailable(WelcomeActivity.this)) {
            Request request = new BasicRequest(WebServerUrlBuilder.getInstance(this).getBaseUrl());
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(this, request, this, 1, false, false, true);
        } else {
            ToastUtils.showShort(getString(R.string.network_error));
            if (application.isLogin()) {
                MainActivity.start(this);
            } else {
                LoginActivity.start(WelcomeActivity.this);
            }
            finish();
        }
    }

    @Override
    public void onSucceed(int what, Response<BasicBean> response) {
        if (what == 1) {
            BasicBean basicBean = response.get();

            if (basicBean == null) {
                return;
            }

            if (basicBean.isSuccess()) {
                MyAddress address = basicBean.parseData(MyAddress.class);
                application.initAddress(address);
                initLaunchPage();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // 异步操作
                        Message message = Message.obtain();
                        message.what = 1;
                        handler.sendMessage(message);
                    }
                }, 3000);

            } else {
                ToastUtils.showShort(basicBean.getMsg());
            }
        }
    }

    private void initLaunchPage() {
        Picasso.with(this)
                .load(AddressManager.getInstance()
                        .getLaunchPageAddress())
                .resize(ScreenUtils.getDisplayWidth()-ScreenUtils.getStatusBarHeight(this)-ScreenUtils.dip2px(122),
                        ScreenUtils.getDisplayHeight())
                .centerInside()
                .placeholder(R.drawable.selector_white)
                .error(R.drawable.selector_white)
                .tag(toString())
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(ivLaunchPage);
    }

    private static class MyHandler extends Handler {

        private final WeakReference<WelcomeActivity> mActivity;

        private MyHandler(WelcomeActivity welcomeActivity) {
            this.mActivity = new WeakReference<>(welcomeActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            WelcomeActivity mInstance = mActivity.get();
            if (mActivity.get() == null){
                return;
            }
            if (msg.what == 1) {
                if (MyApplication.getInstance().isFirst()) {
                    mInstance.startActivity(new Intent(mInstance, GuideActivity.class));
                } else if (MyApplication.getInstance().isLogin()) {
                    MainActivity.start(mInstance);
                } else {
                    LoginActivity.start(mInstance);
                }
                mInstance.finish();
            }
            super.handleMessage(msg);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // i null, all callbacks and messages will be removed
        handler.removeCallbacksAndMessages(null);
    }
}
